Skip to content

implement sort#2446

Open
GorleRajeshBharadwaj wants to merge 11 commits into
mate-academy:masterfrom
GorleRajeshBharadwaj:master
Open

implement sort#2446
GorleRajeshBharadwaj wants to merge 11 commits into
mate-academy:masterfrom
GorleRajeshBharadwaj:master

Conversation

@GorleRajeshBharadwaj
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job setting up the sort2 method on the Array prototype! The structure is a solid start. However, this will require some changes as the core sorting logic is not working correctly and is missing a key feature from the native sort method.

Here are the two main issues to address:

  1. Incorrect Sorting Algorithm: The current implementation has logical flaws in its loops and comparison. The outer loop starts at i = this.length, which causes an error because this[this.length] is undefined. A correct sorting algorithm, like bubble sort, typically compares adjacent elements (e.g., this[j] and this[j+1]) and swaps them if they're out of order.
  2. Missing Default Behavior: The native sort method works even without a compareFunction by defaulting to string comparison. Your implementation will throw an error if compareFunction is undefined. Please add a default comparator to handle this case.

Once these issues are resolved, your custom sort method will be fully functional. Keep up the great effort!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

Comment thread src/arrayMethodSort.js Outdated
@@ -6,7 +6,22 @@
function applyCustomSort() {
[].__proto__.sort2 = function(compareFunction) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The native Array.prototype.sort method has a default behavior for when no compareFunction is provided (it converts elements to strings and compares them). Your implementation will throw an error if called without an argument. Consider adding a default comparator if compareFunction is undefined.

Comment thread src/arrayMethodSort.js Outdated
Comment on lines +9 to +15
for (let i = this.length; i > 1; i--) {
for(let j = 0; j < i; j++) {
if (compareFunction(this[j], this[i]) < 0) {
swap(this, i, j);
}
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sorting algorithm has a couple of logical issues that prevent it from working correctly:

  1. The outer loop starts with i = this.length, which will cause an error because this[this.length] is undefined. Array indices only go up to length - 1.
  2. The comparison logic is flawed. You are comparing each element this[j] against a single element this[i]. A common sorting algorithm like bubble sort works by comparing adjacent elements (e.g., this[j] and this[j + 1]) and swapping them if they are in the wrong order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants